package general.zjitc.net.androidgeneral3;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

/**
 * 步骤：
 * 1.定义主布局activity_main.xml
 * 2.创建menu包，创建main.xml
 * 3.创建每个Activity所对应的fragment.xml
 * 4.创建recyclerView需要的recycler.xml
 * 5.创建recyclerView适配器 RecycclerAdapter
 * 6.创建每个f'ragment相对于的ListFragment
 * 7.创建适配器FragmentPageAdapter
 * 8.定义MainActivity
 */
public class MainActivity extends AppCompatActivity {
    private Toolbar mToolbar;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<Integer> mImage;
    private List<String> mTitiles;
    private List<String> mContent;
    private List<Fragment> mFragments;
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initViews();
    }

    private void initViews() {
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.search:
                        break;
                    case R.id.about:
                        break;
                    case R.id.shop:
                        break;
                        default:break;
                }
                return true;
            }
        });
        mTitiles.add("壁纸推荐");
        mTitiles.add("手机壁纸");
        mTitiles.add("电脑壁纸");
        for (int i = 0; i <mTitiles.size() ; i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(mTitiles.get(i)));
        }
        mContent.add("头像1");
        mContent.add("头像2");
        mContent.add("头像3");
        mContent.add("头像4");

        mImage.add(R.drawable.tp);
        mImage.add(R.drawable.tp2);
        mImage.add(R.drawable.tp3);
        mImage.add(R.drawable.tp4);

        ListFragment listFragment=new ListFragment(mContent,mImage);
        ListFragment2 listFragment2=new ListFragment2();
        ListFragment3 listFragment3=new ListFragment3();

        mFragments.add(listFragment);
        mFragments.add(listFragment2);
        mFragments.add(listFragment3);

        FragmentPagerAdapter fragmentPagerAdapter=new FragmentPageAdapter(getSupportFragmentManager(),mTitiles,mFragments);
        mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.setAdapter(fragmentPagerAdapter);
    }

    private void init() {
        mToolbar=findViewById(R.id.tool);
        mTabLayout=findViewById(R.id.tab);
        mViewPager=findViewById(R.id.viewpage);
        mContent=new ArrayList<>();
        mFragments=new ArrayList<>();
        mImage=new ArrayList<>();
        mTitiles=new ArrayList<>();
    }
}
